package sjtu.ist.warehouse4ws.service;

import java.util.List;

import sjtu.ist.warehouse4ws.dao.UserDaoHibernate;
import sjtu.ist.warehouse4ws.model.User;

public class UserServiceImpl implements UserService{

	private UserDaoHibernate userDao;
	private User user;
	private List<User> users;
	
	public void setUserDao(UserDaoHibernate userDao){
		this.userDao = userDao;
	}
	
	@Override
	public User get(String id) {
		return userDao.get(id);
	}

	@Override
	public void save(User user) {
		userDao.save(user);
	}

	@Override
	public void update(User user) {
		userDao.update(user);
	}

	@Override
	public void delete(String id) {
		userDao.delete(id);
	}

	@Override
	public void delete(User user) {
		userDao.delete(user);
	}

	@Override
	public List<User> findByName(String name) {
		return userDao.findByName(name);
	}

	@Override
	public List<User> findByCompany(String companyName) {
		return userDao.findByCompany(companyName);
	}

	@Override
	public void registUser(String name, String password, String company) {
		user = new User();
		user.setName(name);
		user.setPassword(password);
		user.setCompany(company);
		
		userDao.save(user);
	}

	@Override
	public boolean isRegisted(String name, String password, String company) {
		if(isUserNameRegisted(name, password) || isCompanyNameRegisted(name, password, company)){
			return false;
		}
		return true;
	}

	@Override
	public boolean isUserNameRegisted(String name, String password) {
		users = findByName(name);
		if(!users.isEmpty())
			return true;
		else
			return false;
	}
	
	@Override
	public boolean isUserNameAdd(String name, String password) {
		users = findByName(name);
		if(!users.isEmpty())
			return true;
		else
			return false;
	}

	@Override
	public boolean isCompanyNameRegisted(String name, String password,
			String companyName) {
		users = findByCompany(companyName);
		if(!users.isEmpty())
			return true;
		else
			return false;
	}

	@Override
	public List<User> getUserList(String companyName,int type) {
		return (List<User>)userDao.findUserByCompany(companyName,type);
	}

	
			
}
